package me.zhengjie.modules.system.excel.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.read.listener.ReadListener;
import me.zhengjie.modules.system.domain.Product;
import me.zhengjie.modules.system.excel.vo.ProductVo;
import me.zhengjie.modules.system.repository.ProductRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ProductListener extends AnalysisEventListener<ProductVo> {

    @Autowired
    private ProductRepository productRepository;

    @Override
    public void invoke(ProductVo productVo, AnalysisContext analysisContext) {
        Product p = new Product();
        BeanUtils.copyProperties(productVo, p);
        Product l = productRepository.findByProductCode(productVo.getProductCode());
        if (l !=null) {
            p.setId(l.getId());
        }
        productRepository.save(p);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
